package com.czk.controller.api;


import com.czk.model.hosp.Department;
import com.czk.model.hosp.Hospital;
import com.czk.model.hosp.Schedule;
import com.czk.service.DepartmentService;
import com.czk.service.HospitalService;
import com.czk.service.HospitalSetService;
import com.czk.service.ScheduleService;
import com.czk.utils.ApiUtil;
import com.czk.vo.hosp.DepartmentQueryVo;
import com.czk.vo.hosp.ScheduleQueryVo;
import com.czk.yygh.common.exception.YyghException;
import com.czk.yygh.common.result.Result;
import com.czk.yygh.common.result.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/9 22:21
 */
@RestController
@Api("医院数据")
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;
    @PostMapping("/schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        this.check(request, hospitalSetService);
        Map<String, Object> map = ApiUtil.transferMap(request.getParameterMap());
        String hoscode = (String) map.get("hoscode");
        String hosScheduleId = (String) map.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        this.check(request, hospitalSetService);
        // 医院编号
        Map<String, Object> map = ApiUtil.transferMap(request.getParameterMap());
        String hoscode = (String) map.get("hoscode");
        int page = StringUtils.isEmpty((String) map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        int limit = StringUtils.isEmpty((String) map.get("limit")) ? 1 : Integer.parseInt((String) map.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode((String) map.get("depcode"));
        Page<Schedule> schedulePage = scheduleService.findByPage(page, limit, scheduleQueryVo);
        return Result.ok(schedulePage);
    }

    @ApiOperation("上传排班")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 校验
        this.check(request, hospitalSetService);
        Map<String, Object> map = ApiUtil.transferMap(request.getParameterMap());
        scheduleService.save(map);
        return Result.ok();
    }

    /**
     * 移除部门
     *
     * @param request
     * @return
     */
    @PostMapping("/department/remove")
    @ApiOperation("删除部门")
    public Result removeDepartment(HttpServletRequest request) {
        this.check(request, hospitalSetService);
        Map<String, Object> map = ApiUtil.transferMap(request.getParameterMap());
        String hoscode = (String) map.get("hoscode");
        String depcode = (String) map.get("depcode");
        departmentService.removeDepartment(hoscode, depcode);
        return Result.ok();
    }

    @PostMapping("/department/list")
    @ApiOperation("科室分页查询")
    public Result findDepartment(HttpServletRequest request) {
        this.check(request, hospitalSetService);
        // 医院编号
        Map<String, Object> map = ApiUtil.transferMap(request.getParameterMap());
        String hoscode = (String) map.get("hoscode");
        int page = StringUtils.isEmpty((String) map.get("page")) ? 1 : Integer.parseInt((String) map.get("page"));
        int limit = StringUtils.isEmpty((String) map.get("limit")) ? 1 : Integer.parseInt((String) map.get("limit"));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        // 调用
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * @param request
     * @return
     */
    @PostMapping("/saveDepartment")
    @ApiOperation("上传科室")
    public Result saveDepartment(HttpServletRequest request) {
        this.check(request, hospitalSetService);

        departmentService.save(ApiUtil.transferMap(request.getParameterMap()));
        return Result.ok();
    }

    @PostMapping("/saveHospital")
    @ApiOperation(value = "上传医院数据")
    public Result saveHosp(HttpServletRequest request) {
        this.check(request, hospitalSetService);
        Map<String, Object> paramMap = ApiUtil.transferMap(request.getParameterMap());
        // 传输过程中，图片编码“+”转换为了“ ”需要转回来
        String logoData = (String) paramMap.get("logoData");
        logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        hospitalService.save(paramMap);
        return Result.ok();
    }

    @PostMapping("/hospital/show")
    @ApiOperation("查询医院")
    public Result getHospitalResult(HttpServletRequest request) {
        this.check(request, hospitalSetService);
        Map<String, Object> paramMap = ApiUtil.transferMap(request.getParameterMap());
        // 拿到医院编号
        String hoscode = (String) paramMap.get("hoscode");

        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 校验
     *
     * @param request
     * @param hospitalSetService
     */
    private void check(HttpServletRequest request, HospitalSetService hospitalSetService) {
        Map<String, Object> map = ApiUtil.transferMap(request.getParameterMap());
        // 校验签名
        boolean checkSign = ApiUtil.checkSign(hospitalSetService, map);
        if (!checkSign) {
            // 签名错误
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }
}
